package main

import (
	"errors"
	"fmt"
	"github.com/urfave/cli/v2"
	"io"
	"net/http"
	"os"
)

// 签到 url
const urlSign = "http://www.1050qm.com/plugin.php?id=dsu_paulsign:sign&operation=qiandao&infloat=0&inajax=0"

// 领取任务 url
const urlReceive = "http://www.1000qm.vip/home.php?mod=task&do=apply&id=1"

const userAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36 Edg/124.0.0.0"

const cookie = "3884eOu94BKka2QD9xjkllZLZem3dq4f1g9Pqhs96ASiGA1Q%2BYyHH%2BqY5VPIuI5HoTUGonWuGPoJ2%2FaYU6O6R5vNuYw"

func main() {
	app := &cli.App{
		Name:  "论坛小工具",
		Usage: "签到和红包功能",
		// 配置多组命令
		Commands: []*cli.Command{
			{
				Name:  "sign",
				Usage: "Forum sign-in",
				Action: func(c *cli.Context) error {
					user := c.Args().Slice()
					if len(user) != 3 {
						return errors.New("输入参数有误 请输入Referer")
					}

					referer := user[1]
					// 用账号和密码发请求
					err := tools(referer, urlSign)
					if err != nil {
						fmt.Println("输入信息有误")
						return nil
					}

					fmt.Println("签到成功")
					return nil
				},
			},
			{
				Name:  "receive",
				Usage: "receive task",
				Action: func(c *cli.Context) error {
					user := c.Args().Slice()
					if len(user) != 3 {
						return errors.New("输入参数有误 请输入Referer")
					}

					referer := user[1]
					// 用账号和密码发请求
					err := tools(referer, urlReceive)
					if err != nil {
						fmt.Println("输入信息有误")
						return nil
					}
					fmt.Println("申领任务成功")
					return nil
				},
			},
		},
	}

	err := app.Run(os.Args)
	if err != nil {
		fmt.Println(err)
	}
}

func tools(referer, url string) error {
	req, err := http.NewRequest("GET", url, nil)
	if err != nil {
		return err
	}
	// 设置 cookie
	req.AddCookie(&http.Cookie{
		Name:  "b1KU_2132_auth",
		Value: cookie,
	})

	// 添加 Referer 和 User-Agent
	req.Header.Set("Referer", referer)
	req.Header.Set("User-Agent", userAgent)

	// 发送请求
	client := &http.Client{}
	resp, err := client.Do(req)
	if err != nil {
		return err
	}

	defer func(Body io.ReadCloser) {
		err := Body.Close()
		if err != nil {
			panic(err)
		}
	}(resp.Body)

	return nil
}
